﻿//=============================================================
// Copyright (C) 2012 深圳市中旭企业管理股份有限公司
// 作 者：田海平
// 日 期：2012-6-3
// 描 述: 流程分类
// 版 本：1.00
// 修改历史纪录
// 版 本  修改时间      修改人            修改内容
//=============================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using ZX.WebApp.WorkFlow;
using ZX.BLL.Extend;
using ZX.EHR.Library;
using ZX.BPM.Library;
namespace ZX.WebApp.WebControls
{
    public class CustomSiteMap : DevExpress.Web.ASPxSiteMapControl.ASPxSiteMapControl
    {
        SiteMapNodeCollection sitemapNodes = null;

        public override SiteMapNodeCollection RootNodes
        {
            get
            {
                if (sitemapNodes == null)
                {
                    sitemapNodes = new SiteMapNodeCollection();
                    SqlSiteMapProvider provider = new SqlSiteMapProvider();
                    if (provider.BuildSiteMap() != null)
                    {
                        sitemapNodes.AddRange(provider.BuildSiteMap().ChildNodes);
                    }
                }
                return sitemapNodes;
            }
        }
        protected override void InitInternal()
        {
            base.InitInternal();
        }
    }

    /// <summary>
    /// SqlSiteMapProvider
    /// </summary>
    public class SqlSiteMapProvider : StaticSiteMapProvider
    {
        //调用流程服务接口实例
        CrmWorkFlowService.WorkFlowServiceClient workFlowServiceClient = new CrmWorkFlowService.WorkFlowServiceClient();
        public int satues = WorkFlowAccredTree.satues;//类型状态
        public DatUser daUser = WorkFlowAccredTree.datUser;  //用户信息
        public Guid functionID = WorkFlowAccredTree.functionID; //
        public string entity = WorkFlowAccredTree.entity; //实体名
        public string type = string.Empty; //类型
        // 节点
        private SiteMapNode rootNode;

        // 节点字典表
        private Dictionary<int, SiteMapNode> _nodes = new Dictionary<int, SiteMapNode>();

        // 用于单例模式
        private readonly object _lock = new object();

        /// <summary>
        /// 从持久性存储区加载站点地图信息，并在内存中构建它
        /// </summary>
        /// <returns></returns>
        public override SiteMapNode BuildSiteMap()
        {
          
            lock (_lock)
            {
                // 单例模式的实现
                if (rootNode == null)
                {
                   
                    DataTable dtSiteMap = new DataTable();
                    BaseBussnies Bbussness = new BaseBussnies();
                      if(entity == "V_HF_TaskInstanceClassView")
                      {
                          BHF_TaskInstanceView tv = new BHF_TaskInstanceView();
                          dtSiteMap = tv.GetTaskInstanceClassList(daUser.LoginName).Tables[0];
                      }
                      else
                      {
                        string sqlurl = Bbussness.GetRoleCondition(daUser, entity, functionID);
                        string[] sql = sqlurl.Split(',');
                        if (sql.Count() == 2)
                        {
                            type = sql[1];
                        }
                        DataView dv;
                        switch (satues)
                        {
                            case 1:
                                if (sql.Count() == 2)
                                {
                                    dv = workFlowServiceClient.WorkFlowInstanceClass(sql[0], sql[1]).DefaultView;
                                    dv.Sort = "Caption Asc";
                                    dtSiteMap = dv.ToTable(); //已完成流程分类
                                }
                                else
                                {
                                    dv = workFlowServiceClient.WorkFlowInstanceClass("", "2").DefaultView;
                                    dv.Sort = "Caption Asc";
                                    dtSiteMap = dv.ToTable(); //已完成流程分类
                                }
                                break;
                            case 2:
                                if (sql.Count() == 2)
                                {
                                    dv = workFlowServiceClient.WorkTaskCompleteClass(sql[0], sql[1]).DefaultView;
                                    dv.Sort = "Caption Asc";
                                    dtSiteMap = dv.ToTable(); //已完成的任务分类
                                }
                                else
                                {
                                    dv = workFlowServiceClient.WorkTaskCompleteClass("", "2").DefaultView;
                                    dv.Sort = "Caption Asc";
                                    dtSiteMap = dv.ToTable(); //已完成的任务分类
                                }
                                break;
                            case 3:
                                if (sql.Count() == 2)
                                {
                                    dv = workFlowServiceClient.WorkTaskInstanceClass(sql[0], sql[1]).DefaultView;
                                    dv.Sort = "Caption Asc";
                                    dtSiteMap = dv.ToTable(); //我参与的任务分类
                                }
                                else
                                {
                                    dv = workFlowServiceClient.WorkTaskInstanceClass("", "2").DefaultView;
                                    dv.Sort = "Caption Asc";
                                    dtSiteMap = dv.ToTable(); //我参与的任务分类
                                }
                                break;
                            case 4:
                                if (sql.Count() == 2)
                                {
                                    dv = workFlowServiceClient.GetWorkFlowApplicationClass(sql[0], sql[1]).DefaultView;
                                    dv.Sort = "Caption Asc";
                                    dtSiteMap = dv.ToTable(); //我参申请分类
                                }
                                else
                                {
                                    dv = workFlowServiceClient.GetWorkFlowApplicationClass("", "2").DefaultView;
                                    dv.Sort = "Caption Asc";
                                    dtSiteMap = dv.ToTable(); //我的申请分类
                                }
                                break;
                            case 5:
                                if (sql.Count() == 2)
                                {
                                    string sqlur = string.Empty;
                                    if (sql[0].ToString() != "")
                                    {

                                        sqlur = sql[0] + " and Status <> '3'";
                                    }
                                    else
                                    {
                                        sqlur = sql[0].ToString();
                                    }
                                    dv = workFlowServiceClient.GetWorkFlowApproveClass(sqlur, sql[1]).DefaultView;
                                    dv.Sort = "Caption Asc";
                                    dtSiteMap = dv.ToTable(); //我的审批分类
                                }
                                else
                                {
                                    dv = workFlowServiceClient.GetWorkFlowApproveClass("Status <> '3'", "2").DefaultView;
                                    dv.Sort = "Caption Asc";
                                    dtSiteMap = dv.ToTable(); //我的审批分类
                                }
                                break;
                            case 6:
                                if (sql.Count() == 2)
                                {
                                    string sqlur = string.Empty;
                                    if (sql[0].ToString() != "")
                                    {
                                        sqlur = sql[0] + " and Status = '3'";
                                    }
                                    else
                                    {
                                        sqlur = sql[0].ToString();
                                    }
                                    dv = workFlowServiceClient.GetWorkFlowApproveClass(sqlur, sql[1]).DefaultView;
                                    dv.Sort = "Caption Asc";
                                    dtSiteMap = dv.ToTable(); //我的审批分类
                                }
                                else
                                {
                                    dv = workFlowServiceClient.GetWorkFlowApproveClass("Status = '3'", "2").DefaultView;
                                    dv.Sort = "Caption Asc";
                                    dtSiteMap = dv.ToTable(); //我的审批分类
                                }
                                break;
                            case 7:
                                if (sql.Count() == 2)
                                {
                                    dv = workFlowServiceClient.GetTaskInstanceClass(sql[0], sql[1]).DefaultView;
                                    dv.Sort = "Caption Asc";
                                    dtSiteMap = dv.ToTable(); //待办任务分类
                                }
                                else
                                {
                                    dv = workFlowServiceClient.GetTaskInstanceClass("", "2").DefaultView;
                                    dv.Sort = "Caption Asc";
                                    dtSiteMap = dv.ToTable(); //待办任务分类
                                }
                                break;
                             case 8:
                                if (sql.Count() == 2)
                                {
                                    dv = workFlowServiceClient.GetWorkFlowErrorLogsClass(sql[0], sql[1]).DefaultView;
                                    dv.Sort = "Caption Asc";
                                    dtSiteMap = dv.ToTable(); //异常流程分类
                                }
                                else
                                {
                                    dv = workFlowServiceClient.GetWorkFlowErrorLogsClass("", "2").DefaultView;
                                    dv.Sort = "Caption Asc";
                                    dtSiteMap = dv.ToTable(); //异常流程分类
                                }
                                break;
                             case 9:
                                if (sql.Count() == 2)
                                {
                                    dv = workFlowServiceClient.GetWofkFlowInstanceClass(sql[0], sql[1]).DefaultView;
                                    dv.Sort = "Caption Asc";
                                    dtSiteMap = dv.ToTable(); //异常流程分类
                                }
                                else
                                {
                                    dv = workFlowServiceClient.GetWofkFlowInstanceClass("", "2").DefaultView;
                                    dv.Sort = "Caption Asc";
                                    dtSiteMap = dv.ToTable(); //异常流程分类
                                }
                                break;
                            default:
                                break;
                        }
                      }

                    if (dtSiteMap.Rows.Count > 0)
                    {
                        DataRow newRow = dtSiteMap.NewRow();
                        if (entity == "V_HF_TaskInstanceClassView")
                        {
                           AddChildrenClass(dtSiteMap);
                        }
                        else 
                        {
                            newRow["FatherId"] = "0";
                            newRow["WFClassId"] = "1";
                            newRow["Caption"] = "流程名称";
                            dtSiteMap.Rows.InsertAt(newRow, 0);

                            rootNode = new SiteMapNode(this, "", "", newRow["Caption"].ToString());
                            string rootid = newRow["WFClassId"].ToString();
                            AddNode(rootNode);
                            AddChildren(rootNode, rootid, dtSiteMap);
                        }
                       
                    }
                }
            }
            return rootNode;
        }
        public void AddChildrenClass(DataTable diSiteMap)
        {

            List<DataRow> childRows = new List<DataRow>();
            foreach (DataRow itme in diSiteMap.Rows) //
            {
                 if (childRows.Count == 0)
                 {
                    childRows.Add(itme);
                 }
                 if (childRows.Where(p => p["CategaryName"].ToString() == itme["CategaryName"].ToString()).Count() == 0) //流程类型是否存在
                 {
                     childRows.Add(itme);
                 }
            }
            int urls = 0;
            foreach (DataRow row in childRows)
            {
                urls++;
                if (urls == 1)
                {
                    //newRow["CategaryName"] = "流程名称1";
                    //dtSiteMap.Rows.InsertAt(newRow, 0);

                    rootNode = new SiteMapNode(this, "", "", row["CategaryName"].ToString());
                    AddNode(rootNode);
                }
                SiteMapNode childnode = new SiteMapNode(this, urls.ToString(), "", row["CategaryName"].ToString());
                string rowid = row["CategaryID"].ToString();
                AddNode(childnode, rootNode);
                AddChildrensClass(childnode, rowid, diSiteMap);
            }
        }
        public void AddChildrensClass(SiteMapNode childnode,string rowid, DataTable diSiteMap)
        {
            List<DataRow> childRows = new List<DataRow>();
            foreach (DataRow itme in diSiteMap.Select("CategaryID = '" + rowid + "'"))
            {
                if (childRows.Count == 0)
                {
                    childRows.Add(itme);
                }
                else if (childRows.Where(p => p["WorkflowID"].ToString() == itme["WorkflowID"].ToString()).Count() == 0) //流程名称是否存在
                {
                    childRows.Add(itme);
                }
            }
            foreach (DataRow row in childRows)
            {
                string urls = string.Empty;
                urls = "~/Extend/YUIList.aspx?entity=V_HF_TaskInstanceView&functionID=8E3F1DF7-DCA8-4110-9446-5E8F9EAC9AAF&condition=WorkFlowId='" + row["WorkFlowId"] + "'&order=taskStartTime desc"; //待办任务
                SiteMapNode childnodes = new SiteMapNode(this, row["WorkflowID"].ToString(), urls, row["WorkflowName"].ToString(), row["UnReadCount"].ToString() + "/" + row["Total"].ToString());
                AddNode(childnodes, childnode);
            }
        }
        public void AddChildren(SiteMapNode rootNode, string rootid, DataTable diSiteMap)
        {

            List<DataRow> childRows = new List<DataRow>();
            foreach (DataRow itme in diSiteMap.Rows) //
            {
                if (itme["FatherId"].ToString() == rootid)
                {
                    if (childRows.Count == 0)
                    {
                        childRows.Add(itme);
                    }
                    if (childRows.Where(p => p["Caption"].ToString() == itme["Caption"].ToString()).Count() == 0) //流程类型是否存在
                    {
                        childRows.Add(itme);
                    }
                }
            }
            int urls = 0;
            foreach (DataRow row in childRows)
            {
                urls++;
                SiteMapNode childnode = new SiteMapNode(this, urls.ToString(), "", row["Caption"].ToString());
                string rowid = row["WFClassId"].ToString();
                AddNode(childnode, rootNode);
                AddChildrens(childnode, rowid, diSiteMap);
            }
        }
        public void AddChildrens(SiteMapNode childnode, string rootid, DataTable diSiteMap)
        {
            List<DataRow> childRows = new List<DataRow>();
            foreach (DataRow itme in diSiteMap.Select("WFClassId = '" + rootid + "'"))
            {

                if (childRows.Count == 0)
                {
                    childRows.Add(itme);
                }
                else if (childRows.Where(p => p["WorkFlowId"].ToString() == itme["WorkFlowId"].ToString()).Count() == 0) //流程名称是否存在
                {
                    childRows.Add(itme);
                }

            }
            foreach (DataRow row in childRows)
            {
                List<DataRow> rows = new List<DataRow>();
                rows = diSiteMap.Select("WorkFlowId = '" + row["WorkFlowId"] + "' and Userid='" + daUser.LoginName + "'").ToList();
                List<DataRow> drow = new List<DataRow>();
                List<DataRow> drowIs = new List<DataRow>();
                foreach (DataRow rowes in rows)
                {
                    if (drow.Count() == 0)
                    {
                        if (satues != 8)
                        {
                            if (rowes["IsDisplay"].ToString() == "" && rowes["IsDisplay"].ToString().Split(',').Where(p => p == daUser.LoginName).Count() == 0)
                                drowIs.Add(rowes);
                        }
                        else
                        {
                            drowIs.Add(rowes);
                        }
                        drow.Add(rowes);
                    }
                    else if (satues == 2 || satues == 3)
                    {
                        if (type == "0")
                        {
                            if (satues == 5 || satues == 6)
                            {
                                if (drow.Where(p => p["WorkTaskInstanceId"].ToString() == rowes["WorkTaskInstanceId"].ToString()).Count() == 0) //流程名称是否存在
                                {
                                    if (satues != 8)
                                    {
                                        if (rowes["IsDisplay"].ToString() == "" && rowes["IsDisplay"].ToString().Split(',').Where(p => p == daUser.LoginName).Count() == 0)
                                            drowIs.Add(rowes);
                                    }
                                    else
                                    {
                                        drowIs.Add(rowes);
                                    }
                                    drow.Add(rowes);
                                }
                            }
                            else
                            {
                                if (drow.Where(p => p["WorkFlowInstanceId"].ToString() == rowes["WorkFlowInstanceId"].ToString()).Count() == 0) //流程名称是否存在
                                {
                                    if (satues != 8)
                                    {
                                        if (rowes["IsDisplay"].ToString() == "" && rowes["IsDisplay"].ToString().Split(',').Where(p => p == daUser.LoginName).Count() == 0)
                                            drowIs.Add(rowes);
                                    }
                                    else
                                    {
                                        drowIs.Add(rowes);
                                    }
                                    drow.Add(rowes);
                                }
                            }
                        }
                        else
                        {
                            if (satues != 8)
                            {
                                if (rowes["IsDisplay"].ToString() == "" && rowes["IsDisplay"].ToString().Split(',').Where(p => p == daUser.LoginName).Count() == 0)
                                    drowIs.Add(rowes);
                            }
                            else
                            {
                                drowIs.Add(rowes);
                            }
                            drow.Add(rowes);
                        }
                    }
                    else
                    {
                        if (satues == 5 || satues == 6)
                        {
                            if (drow.Where(p => p["WorkTaskInstanceId"].ToString() == rowes["WorkTaskInstanceId"].ToString()).Count() == 0) //流程名称是否存在
                            {
                                if (satues != 8)
                                {
                                    if (rowes["IsDisplay"].ToString() == "" && rowes["IsDisplay"].ToString().Split(',').Where(p => p == daUser.LoginName).Count() == 0)
                                        drowIs.Add(rowes);
                                }
                                else 
                                {
                                    drowIs.Add(rowes);
                                }
                                drow.Add(rowes);
                            }
                        }
                        else
                        {
                            if (drow.Where(p => p["WorkFlowInstanceId"].ToString() == rowes["WorkFlowInstanceId"].ToString()).Count() == 0) //流程名称是否存在
                            {
                                if (satues != 8)
                                {
                                    if (rowes["IsDisplay"].ToString() == "" && rowes["IsDisplay"].ToString().Split(',').Where(p => p == daUser.LoginName).Count() == 0)
                                        drowIs.Add(rowes);
                                }
                                else 
                                {
                                    drowIs.Add(rowes);
                                }
                                drow.Add(rowes);
                            }
                        }
                    }
                }
                string urls = string.Empty;
                switch (satues)
                {
                    case 1:
                        urls = "~/Extend/YUIList.aspx?entity=HF_WorkFlowInstanceView&functionID=05da08d8-d3d7-41a2-ab0a-c4cc276dc97d&condition=WorkFlowId='" + row["WorkFlowId"] + "' and Status='3'&order=EndTime desc";//已完成流程分类
                        break;
                    case 2:
                        urls = "~/Extend/YUIList.aspx?entity=V_HF_CompleteWorkTask&functionID=17eae1ad-6f95-44bb-a231-3bfb8276ae83&condition=WorkFlowId='" + row["WorkFlowId"] + "'";//已完成的任务分类
                        break;
                    case 3:
                        urls = "~/Extend/YUIList.aspx?entity=V_HF_MyAllWorkTask&functionID=17eae1ad-6f95-44bb-a231-3bfb8276ae84&condition=WorkFlowId='" + row["WorkFlowId"] + "'"; //我参与的任务分类
                        break;
                    case 4:
                        urls = "~/Extend/YUIList.aspx?entity=HF_WorkFlowApplicationView&functionID=818d19f0-ffbf-4fc8-a624-ded200c3b151&condition=WorkFlowId='" + row["WorkFlowId"] + "'&order=OperDateTime desc"; //我的申请
                        break;
                    case 5:
                        urls = "~/Extend/YUIList.aspx?entity=HF_WorkFlowApproveView&functionID=4d2286ce-ba11-49c5-8ab2-d6b8b5ad613f&condition=WorkFlowId='" + row["WorkFlowId"] + "' and Status<>'3'&order=OperDateTime desc"; //我的审批()
                        break;
                    case 6:
                        urls = "~/Extend/YUIList.aspx?entity=HF_WorkFlowApproveView&functionID=F2EACCB3-5CA4-47BC-B4E2-E4F51545FE63&condition=WorkFlowId='" + row["WorkFlowId"] + "' and Status='3'&order=EndTime desc"; //我的审批(已完成流程)
                        break;
                    case 7:
                        urls = "~/Extend/YUIList.aspx?entity=V_HF_TaskInstanceView&functionID=8E3F1DF7-DCA8-4110-9446-5E8F9EAC9AAF&condition=WorkFlowId='" + row["WorkFlowId"] + "'&order=taskStartTime desc"; //待办任务
                        break;
                    case 8:
                        urls = "~/Extend/YUIList.aspx?entity=HF_ErrorLogsView&functionID=F20C4FF5-E920-40FA-BC0A-6C3294DA9FC3&condition=WorkFlowId='" + row["WorkFlowId"] + "' and OperContentText IS NOT NULL and LogSatus ='1'&order=LogDatetime desc"; //异常流程列表
                        break;
                    case 9:
                        urls = "~/Extend/YUIList.aspx?entity=HF_WorkTaskInstanceCloseView&functionID=79C06208-96FB-4DB5-B21B-7BBBC17C23B7&condition=WorkFlowId='" + row["WorkFlowId"] + "'&order=OperDateTime desc"; //异常流程列表
                        break;
                    default:
                        break;
                }
                SiteMapNode childnodes = new SiteMapNode(this, row["WorkTaskInstanceId"].ToString(), urls, row["FlowCaption"].ToString(), drowIs.Count.ToString() + "/" + drow.Count.ToString());
                AddNode(childnodes, childnode);
            }
        }
        protected override SiteMapNode GetRootNodeCore()
        {
           
            lock (_lock)
            {
                return BuildSiteMap();
            }
        }
        /// <summary>
        /// 清除站点地图中的节点。
        /// </summary>
        protected override void Clear()
        {
            lock (_lock)
            {
                _nodes = new Dictionary<int, SiteMapNode>();
                base.Clear();
            }
        }
    }
}